﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Composite.Data;

public partial class Admin_LCASystem_Controls_InputModal : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void RefreshComponents(Guid? SystemId)
    {
        using (DataConnection conn = new DataConnection())
        {
            //var units = from u in conn.Get<LCA.Data.Unit>()
            //            join uc in conn.Get<LCA.Data.UnitGroup>() on u.UnitGroup equals uc.Id
            //            select new
            //            {
            //                Id = u.Id,
            //                Name = uc.Name + " - " + u.Name
            //            };
            //units = units.OrderBy(x => x.Name);

            //ddlUnitModal.DataSource = units;
            //ddlUnitModal.DataBind();
            //ddlUnitModal.Items.Insert(0, new ListItem("-- Please Select --", "NULL"));

            var items = (from f in conn.Get<LCA.Data.Flow>()
                         where f.IsDeleted == false
                         orderby f.Name
                         select new
                         {
                             Name = "Flow - " + f.Name,
                             Value = "Flow|" + f.Id.ToString().ToLower()
                         }).ToList();

            var systemItems = (from f in conn.Get<LCA.Data.LCASystem>()
                               where f.IsDeleted == false
                               orderby f.Name
                               select new
                               {
                                   
                                   Name = "System - " + f.Name,
                                   Value = "System|" + f.Id.ToString().ToLower()
                               }).ToList();

            // Remove itself system, to avoid recursion calculation
            if (SystemId.HasValue)
            {
                var selfItem = systemItems.Single(x => x.Value.Contains("|" + SystemId.Value.ToString().ToLower()));
                if (selfItem != null)
                    systemItems.Remove(selfItem);
            }

            items.AddRange(systemItems);
            ddlFlowSystem.DataSource = items;
            ddlFlowSystem.DataBind();
        }
        
    }
}